package com.taotao.store.order.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.store.order.mapper.OrderMapper;
import com.taotao.store.order.pojo.Order;

@Service
public class OrderService {

	@Autowired
	private OrderMapper orderMapper;

	public String createOrder(Order order) {

		// 生成订单ID，规则为：userid+当前时间戳
		String orderId = order.getUserId() + "" + System.currentTimeMillis();
		order.setOrderId(orderId);

		// 设置订单的初始状态为未付款
		order.setStatus(1);

		// 设置订单的创建时间
		order.setCreateTime(new Date());
		order.setUpdateTime(order.getCreateTime());

		// 设置买家评价状态，初始为未评价
		order.setBuyerRate(0);

		// 持久化order对象
		return this.orderMapper.save(order) == 1? orderId : null;
	}

	public Order queryOrderById(String orderId) {
		return this.orderMapper.queryOrderById(orderId);
	}

	public EasyUIResult queryOrderByUserNameAndPage(String buyerNick, int page, int count) {
		PageHelper.startPage(page, count);
		List<Order> orderList = this.orderMapper.queryOrderByBuyerNickSortByCreated(buyerNick);
		PageInfo<Order> pageInfo = new PageInfo<>(orderList);
		return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
	}

	public Boolean changeOrderStatus(Order order) {
		switch(order.getStatus()){
		case 1:
			break;
		case 2:
			order.setPaymentTime(new Date());
			break;
		case 3:
			break;
		case 4:
			order.setConsignTime(new Date());
			break;
		case 5:
			order.setEndTime(new Date());
			break;
		case 6:
			order.setCloseTime(new Date());
			break;
		
		}
		order.setUpdateTime(new Date());
		return this.orderMapper.update(order) == 1;
	}

}
